#include<DFRobot_ID809.h>
#include <DFRobot_ASR_M.h>
#include <DFRobot_Servo.h>
#include <DFRobot_SHT3x.h>
#include <DFRobot_NFC0231.h>
#include <DFRobot_NeoPixel.h>
#include <DFRobot_HuskyLens.h>
#include <DFRobot_LiquidCrystal_I2C.h>
#include <DFRobot_SpeechSynthesis_M.h>

// 动态变量
String         mind_s_NFCcard01_success, mind_s_NFCcard02_wrong_;
volatile float mind_n_fingerprint, mind_n_wendu_shidu_reboot_key, mind_n_angle_degree_to_servo,
               mind_n_MIC_INPUT, mind_n_temprature, mind_n_wet_value;
// 函数声明
void DF_ZhiWenChuShiHua();
void DF_FIRST_FINGER_LOAD_1602();
void DF_ShuChuWenDuHeShiDu();
void DF_SECOND_FINGER_LOAD_1602();
void DF_THIRD_FINGER_LOAD_1602();
void DF_CAMERA();
// 创建对象
DFRobot_ID809_IIC           ID809fingerprint(&Wire,0x1f);
DFRobot_LiquidCrystal_I2C   lcd1602;
DFRobot_SpeechSynthesis_I2C sstts;
DFRobot_ASR                 asr;
DFRobot_PN532_IIC           nfc;
Servo                       servo_3;
DFRobot_NeoPixel            neoPixel_13;
DFRobot_SHT3x               sht3x(&Wire,0x45,4);
DFRobot_HuskyLens           huskylens;
DFRobot_NeoPixel            neoPixel_4;


// 主程序开始
void setup() {
	Serial.begin(9600);
	ID809fingerprint.begin();
	servo_3.attach(3);
	while (sht3x.begin() != 0);
	neoPixel_4.begin(4, 5, 255);
	mind_n_wendu_shidu_reboot_key = 0;
	sstts.begin(sstts.eV2);
	asr.begin(LOOP, MIC);
	lcd1602.begin(0x3e);
	asr.addCommand("hu jiao hu shi", 1);
	asr.addCommand("kai qi yue du mo shi", 2);
	asr.addCommand("guan bi suo you deng guang", 4);
	mind_s_NFCcard01_success = "31ab5f0f";
	mind_s_NFCcard02_wrong_ = "41016f0f";
	nfc.begin();
	asr.start();
	servo_3.angle(abs(0));
	DF_ZhiWenChuShiHua();
	neoPixel_13.begin(13, 7);
	sstts.setVolume(8);
	sstts.setSpeed(5);
	sstts.setSoundType(sstts.eMale1);
	sstts.setTone(5);
	huskylens.beginI2CUntilSuccess();
	huskylens.writeAlgorithm(ALGORITHM_FACE_RECOGNITION);
	sstts.speak("初始化完成");
}
void loop() {
	DF_ShuChuWenDuHeShiDu();
	mind_n_angle_degree_to_servo = (map(analogRead(A0), 0, 1023, 0, 90));
	mind_n_MIC_INPUT = asr.read();
	if ((mind_n_MIC_INPUT==1)) {
		lcd1602.clear();
		sstts.speak("已呼叫");
		neoPixel_4.setBrightness(157);
		lcd1602.printLine(uint32_t(1), "ok");
	}
	if ((mind_n_MIC_INPUT==2)) {
		neoPixel_13.setRangeColor(0, 6, 0xFFFF00);
	}
	if ((mind_n_MIC_INPUT==3)) {
		neoPixel_13.setRangeColor(0, 6, 0xFFFFFF);
	}
	if ((mind_n_MIC_INPUT==4)) {
		neoPixel_13.clear();
	}
	Serial.println(mind_n_angle_degree_to_servo);
	DF_CAMERA();
	Serial.println(mind_n_angle_degree_to_servo);
}


// 自定义函数
void DF_ZhiWenChuShiHua() {
	mind_n_fingerprint = 0;
	for (int index = 0; index < 10; index++) {
		ID809fingerprint.delFingerprint(mind_n_fingerprint);
		mind_n_fingerprint += 1;
	}
	mind_n_fingerprint = 0;
	for (int index = 0; index < 1; index++) {
		DF_FIRST_FINGER_LOAD_1602();
		while (!(!(ID809fingerprint.detectFinger()))) {}
		DF_SECOND_FINGER_LOAD_1602();
		while (!(!(ID809fingerprint.detectFinger()))) {}
		DF_THIRD_FINGER_LOAD_1602();
		while (!(!(ID809fingerprint.detectFinger()))) {}
		mind_n_fingerprint = (ID809fingerprint.getEmptyID());
		ID809fingerprint.storeFingerprint(mind_n_fingerprint);
		if ((ID809fingerprint.getStatusID(mind_n_fingerprint))) {
			lcd1602.clear();
			lcd1602.printLine(uint32_t(1), "LOAD SUCCESS");
		}
		else {
			lcd1602.clear();
			lcd1602.printLine(uint32_t(1), "LOAD FAILED");
		}
	}
}
void DF_FIRST_FINGER_LOAD_1602() {
	lcd1602.clear();
	lcd1602.printLine(uint32_t(1), "ONLOAD FINGER PR");
	lcd1602.printLine(uint32_t(2), "INT TIME 01");
	ID809fingerprint.collectionFingerprint(15);
	lcd1602.clear();
	lcd1602.printLine(uint32_t(1), "TIME 01 FINA");
}
void DF_ShuChuWenDuHeShiDu() {
	mind_n_wendu_shidu_reboot_key += 1;
	if (((mind_n_wendu_shidu_reboot_key==100) || (mind_n_wendu_shidu_reboot_key==1))) {
		mind_n_temprature = (round((sht3x.getTemperatureC())));
		mind_n_wet_value = (round((sht3x.getHumidityRH())));
		lcd1602.clear();
		lcd1602.print(uint32_t(0), uint32_t(0), mind_n_temprature);
		lcd1602.print(uint32_t(0), uint32_t(1), mind_n_wet_value);
		lcd1602.print(uint32_t(3), uint32_t(0), "0C");
		lcd1602.print(uint32_t(3), uint32_t(1), "RH");
		if ((mind_n_temprature>=32)) {
			sstts.speak("高温预警，请及时处理");
		}
		if ((mind_n_wet_value>=70)) {
			sstts.speak("高环境湿度预警，请及时处理");
		}
	}
	if ((mind_n_wendu_shidu_reboot_key==101)) {
		mind_n_wendu_shidu_reboot_key = 0;
	}
	servo_3.angle(abs(mind_n_angle_degree_to_servo));
}
void DF_SECOND_FINGER_LOAD_1602() {
	lcd1602.clear();
	lcd1602.printLine(uint32_t(1), "ONLOAD FINGER PR");
	lcd1602.printLine(uint32_t(2), "INT TIME 02");
	ID809fingerprint.collectionFingerprint(15);
	lcd1602.clear();
	lcd1602.printLine(uint32_t(1), "TIME 02 FINA");
}
void DF_THIRD_FINGER_LOAD_1602() {
	lcd1602.clear();
	lcd1602.printLine(uint32_t(1), "ONLOAD FINGER PR");
	lcd1602.printLine(uint32_t(2), "INT TIME 03");
	ID809fingerprint.collectionFingerprint(15);
	lcd1602.clear();
	lcd1602.printLine(uint32_t(1), "TIME 03 FINA");
}
void DF_CAMERA() {
	huskylens.request();
	if (huskylens.isAppearDirect(HUSKYLENSResultBlock)) {
		if (huskylens.isAppear(1,HUSKYLENSResultBlock)) {
			sstts.speak("你好，病人侯家盛");
		}
	}
}
